﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using dotNails.CodeGen.Models;

namespace dotNails.CodeGen.Models.dotNails {
	/// <summary>
	/// Summary description for ControllerTemplate.
	/// </summary>
	public class Controller : CodeBlock {
		private Table Table { get; set; }
		private string DataContextClass { get; set; }
		private string _controllerNamespace = string.Empty;
		private string _modelNamespace = string.Empty;
		private string _commonNamespace = string.Empty;

		public Controller(Table table, string dataContextClass, string controllerNamespace, string modelNamespace, string commonNamespace) {
			Table = table;
			TemplateFileName = Path.Combine(TemplatePath, "Controller.cst");
			DataContextClass = dataContextClass;
			_controllerNamespace = controllerNamespace;
			_modelNamespace = modelNamespace;
			_commonNamespace = commonNamespace;
		}
		
		public override void PopulateTemplate(BaseCodeTemplate template) {
			base.PopulateTemplate(template);

			ControllerTemplate controllerTemplate = template as ControllerTemplate;

			if (controllerTemplate != null) {
				// Populate properties on the base template
				controllerTemplate.EntityBaseClass = "Controller";
				controllerTemplate.Namespace = _controllerNamespace;
				controllerTemplate.EntityClass = Table.Type.Name;
				controllerTemplate.Table = Table;
				controllerTemplate.DataContextClass = DataContextClass;
				controllerTemplate.ModelNamespace = _modelNamespace;
				controllerTemplate.ProceduresClass = DataContextClass.Replace("DataContext", "Procedures");
				controllerTemplate.CommonNamespace = _commonNamespace;
			}
			else {
				throw new Exception("Template is not of type ControllerTemplate.  Check the Controller.cst file to make sure it inherits from ControllerTemplate");
			}
		}
	}
}